<?php
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\AdminService;
use app\common\controller\Backend;

/**
 * 人员管理-控制器
 * @author 牧羊人
 * @since 2020/7/11
 * Class Admin
 * @package app\admin\controller
 */
class Admin extends Backend
{
    /**
     * 初始化
     * @author 牧羊人
     * @since 2020/7/11
     */
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\Admin();
        $this->service = new AdminService();
        $this->validate = new \app\admin\validate\Admin();
    }

    /**
     * 添加或编辑
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author 牧羊人
     * @since 2020/7/11
     */
    public function edit()
    {
        // 获取上级部门
        $deptMod = new \app\admin\model\Dep();
        $result = $deptMod->getChilds(0, true);
        if ($result) {
            $deptList = array();
            foreach ($result as $val) {
                $key = (int)$val['id'];
                $deptList[$key] = $val;
                $vlist = isset($val['children']) ? $val['children'] : [];
                if ($vlist) {
                    foreach ($vlist as &$v) {
                        $k = (int)$v['id'];
                        $v['name'] = "|--" . $v['name'];
                        $deptList[$k] = $v;
                        $clist = isset($v['children']) ? $v['children'] : [];
                        if ($clist) {
                            foreach ($clist as &$vt) {
                                $kt = (int)$vt['id'];
                                $vt['name'] = "|--|--" . $vt['name'];
                                $deptList[$kt] = $vt;
                            }
                        }
                    }
                }
            }
        }
        // 绑定数据
        $this->assign("deptList", $deptList);
        return parent::edit(); // TODO: Change the autogenerated stub
    }

    /**
     * 重置密码
     * @return mixed
     * @author 牧羊人
     * @date 2019/5/10
     */
    public function resetPwd()
    {
        if (IS_POST) {
            $result = $this->service->resetPwd();
            return $result;
        }
    }

    /**
     * 删除人员
     * @return array
     * @since 2021/7/7
     * @author 牧羊人
     */
    public function drop()
    {
        if (IS_POST) {
            $id = input('post.id');
            $info = $this->model->getInfo($id);
            if ($info) {
                $result = $this->model->drop($id);
                if ($result !== false) {
                    // 删除用户整体缓存
                    $this->model->cacheDAll();
                    return message();
                }
            }
            return message($this->model->getError(), false);
        }
        return parent::drop(); // TODO: Change the autogenerated stub
    }
}
